package com.easyj.admin.activiti.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletResponse;

import com.easyj.admin.activiti.service.ProcessImagetService;
import com.easyj.base.common.ajax.Result;
import com.easyj.base.controller.BaseController;
import com.easyj.base.exception.ActivitiException;

/**
 * 
 * @author hjf
   *      流程实例
 */
@RestController
@RequestMapping(value = "/processImage")
public class ProcessImageController extends BaseController {


	
	@Autowired
	private ProcessImagetService  processInstService;
	
	/**
	 * 适应前后端分离
     * 显示带跟踪的图片，返回base64 编码图片
     * procinstId 流程ID
     */
    @RequestMapping(value = "getDiagram", method = RequestMethod.GET)
    public Result getDiagram( @RequestParam(value = "procInstId")String procInstId) throws ActivitiException{
    	    String base64str=processInstService.getDiagram(procInstId);
	        return Result.Success(base64str);
    	
    }
	
	
	 /**
     * 显示带跟踪的图片，HttpServletResponse 输出流
     * procinstId 流程ID
     */
    @RequestMapping(value = "showDiagram", method = RequestMethod.GET)
    public void showDiagram( @RequestParam(value = "procInstId")String procInstId, HttpServletResponse response) {
    	InputStream imageStream=null;
    	try {	
 
	        imageStream = processInstService.showDiagram(procInstId);
	        response.setContentType("image/svg+xml");
	        //response.setContentType("image/png");
	        // 输出资源内容到相应对象
	        byte[] b = new byte[1024];
	        int len;
	        while ((len = imageStream.read(b, 0, 1024)) != -1) {
	            response.getOutputStream().write(b, 0, len);
	        }
	        
      }catch(Exception ex) {
    	  log.error("generate Diagram error", ex);
      }finally{
    	  if(imageStream!=null) {
    		  try {
				imageStream.close();
			} catch (IOException e) {
				
			}
    	  }
      }
    }
    
    
   
}
